﻿package cn.flashk.image
{
    import cn.flashk.events.*;
    import flash.events.*;

    public class AnimationGridBitmap extends GridBitmap
    {
        protected var _playSpeed:Number = 1;
        public var isUseFrameCompensation:Boolean = false;
        private var endFrame:uint;
        private var isCheckEnd:Boolean = false;
        protected var nowFrame:Number = 0;
        protected var isUserCall:Boolean = true;

        public function AnimationGridBitmap()
        {
            return;
        }// end function

        public function set playSpeed(param1:Number) : void
        {
            _playSpeed = param1;
            if (_playSpeed < 0)
            {
                _playSpeed = 1;
            }
            return;
        }// end function

        public function get playSpeed() : Number
        {
            return _playSpeed;
        }// end function

        public function get currentFrame() : uint
        {
            return uint(nowFrame);
        }// end function

        public function play() : void
        {
            isCheckEnd = false;
            this.removeEventListener(Event.ENTER_FRAME, prevFrame);
            this.addEventListener(Event.ENTER_FRAME, nextFrame);
            return;
        }// end function

        public function backPlay() : void
        {
            isCheckEnd = false;
            this.removeEventListener(Event.ENTER_FRAME, nextFrame);
            this.addEventListener(Event.ENTER_FRAME, prevFrame);
            return;
        }// end function

        public function gotoAndPlay(param1:uint) : void
        {
            if (param1 > totalFrames)
            {
                param1 = totalFrames;
            }
            viewByFrame(param1);
            nowFrame = param1 - 1;
            play();
            return;
        }// end function

        public function gotoAndBackPlay(param1:uint) : void
        {
            if (param1 > totalFrames)
            {
                param1 = totalFrames;
            }
            viewByFrame(param1);
            nowFrame = param1 + 1;
            backPlay();
            return;
        }// end function

        public function gotoAndPlayThenStop(param1:uint, param2:uint = 0) : void
        {
            endFrame = param2;
            if (param2 == 0)
            {
                endFrame = totalFrames;
            }
            if (endFrame < 1)
            {
                endFrame = 1;
            }
            if (endFrame > totalFrames)
            {
                endFrame = totalFrames;
            }
            gotoAndPlay(param1);
            isCheckEnd = true;
            return;
        }// end function

        public function gotoAndBackPlayThenStop(param1:uint, param2:uint = 0) : void
        {
            endFrame = param2;
            if (endFrame < 1)
            {
                endFrame = 1;
            }
            if (endFrame > totalFrames)
            {
                endFrame = totalFrames;
            }
            gotoAndBackPlay(param1);
            isCheckEnd = true;
            return;
        }// end function

        public function gotoAndStop(param1:uint) : void
        {
            nowFrame = param1;
            checkAndResetNowFrame();
            this.removeEventListener(Event.ENTER_FRAME, prevFrame);
            this.removeEventListener(Event.ENTER_FRAME, nextFrame);
            viewByFrame(nowFrame);
            return;
        }// end function

        public function pause() : void
        {
            this.removeEventListener(Event.ENTER_FRAME, prevFrame);
            this.removeEventListener(Event.ENTER_FRAME, nextFrame);
            return;
        }// end function

        public function stop() : void
        {
            this.removeEventListener(Event.ENTER_FRAME, prevFrame);
            this.removeEventListener(Event.ENTER_FRAME, nextFrame);
            nowFrame = 1;
            viewByFrame(nowFrame);
            return;
        }// end function

        public function nextFrame(event:Event = null) : void
        {
            nowFrame = nowFrame + playSpeed;
            if (nowFrame > totalFrames)
            {
                nowFrame = nowFrame % totalFrames;
            }
            isUserCall = false;
            if (nowFrame < 1 && nowFrame > 0)
            {
                return;
            }
            viewByFrame(nowFrame);
            if (BitmapTool.getNoAlphaArea(this.bitmapData, true).width == 0)
            {
                nextFrame();
            }
            if (isCheckEnd == true && uint(nowFrame) == endFrame)
            {
                this.removeEventListener(Event.ENTER_FRAME, nextFrame);
                this.dispatchEvent(new MovieEvent(MovieEvent.PLAY_ENDED));
            }
            return;
        }// end function

        public function prevFrame(event:Event = null) : void
        {
            nowFrame = nowFrame - playSpeed;
            if (nowFrame < 1)
            {
                nowFrame = totalFrames + nowFrame;
            }
            isUserCall = false;
            if (nowFrame > totalFrames)
            {
                return;
            }
            if (nowFrame == uint(nowFrame) + 0.5)
            {
                return;
            }
            viewByFrame(nowFrame);
            if (isCheckEnd == true && uint(nowFrame) == endFrame)
            {
                this.removeEventListener(Event.ENTER_FRAME, prevFrame);
                this.dispatchEvent(new MovieEvent(MovieEvent.BACK_PLAY_ENDED));
            }
            return;
        }// end function

        private function checkAndResetNowFrame() : void
        {
            if (nowFrame < 1)
            {
                nowFrame = 1;
            }
            if (nowFrame > totalFrames)
            {
                nowFrame = totalFrames;
            }
            return;
        }// end function

        override public function viewByXY(param1:uint, param2:uint) : void
        {
            super.viewByXY(param1, param2);
            if (isUserCall == true)
            {
                this.removeEventListener(Event.ENTER_FRAME, prevFrame);
                this.removeEventListener(Event.ENTER_FRAME, nextFrame);
            }
            isUserCall = true;
            return;
        }// end function

        override public function viewByFrame(param1:uint) : void
        {
            if (isUserCall == true)
            {
                nowFrame = param1;
                this.removeEventListener(Event.ENTER_FRAME, prevFrame);
                this.removeEventListener(Event.ENTER_FRAME, nextFrame);
            }
            super.viewByFrame(param1);
            isUserCall = true;
            return;
        }// end function

    }
}
